﻿using System.Linq;
using System.Web.UI.WebControls;
using Inkubator.Wizard.Model.Model;
using Inkubator.Wizard.WizardGenerator.Validator;
using Inkubator.Wizard.WebWizard.WizardControl;

namespace Inkubator.Wizard.WizardGenerator.Generator
{
    /// <summary>
    /// This class design the structure of each StepWizard object.
    /// </summary>
    public class StepDesigner
    {
        private CreateControl createControl;
        private WizardValidator validator;
        private StepWizard stepWizard;

        public StepDesigner()
        {
            createControl = new CreateControl();
            validator = new WizardValidator();
            stepWizard = new StepWizard();
        }

        /// <summary>
        /// This method is the one who managed the process to add the respective
        /// WebControls to each step.
        /// </summary>
        /// <param name="step"></param>
        /// <returns></returns>
        public StepWizard addStepControls(Step step)
        {
            stepWizard.setDescription(step.description);
            stepWizard.setName(step.name);

            for (int i = 0; i < step.layouts.Count; i++)
            {
                Layout layout = step.layouts.ElementAt(i);

                this.manageText(layout);
                this.manageGroup(layout);
                this.manageDate(layout);
                this.manageTable(layout);
            }

            return stepWizard;
        }

        /// <summary>
        /// Add a TextBox Component to the current step.
        /// </summary>
        /// <param name="layout"></param>
        private void manageText(Layout layout)
        {

            for (int i = 0; i < layout.textArray.Length; i++)
            {
                Label label = new Label();
                label.Text = layout.textArray.ElementAt(i).label;

                TextBox text = (TextBox) createControl.obtainControl(layout.textArray.ElementAt(i));
                this.stepWizard.addIdControl(layout.textArray.ElementAt(i).ID);

                this.stepWizard.addComponent(label, this.addValidator(layout.textArray.ElementAt(i), text));
            }

        }

        /// <summary>
        /// Add a ListControl Component to the current step.
        /// </summary>
        /// <param name="layout"></param>
        private void manageGroup(Layout layout)
        {

            for (int i = 0; i < layout.groupArray.Length; i++)
            {
                Label label = new Label();
                label.Text = layout.groupArray.ElementAt(i).label;

                ListControl group = (ListControl) createControl.obtainControl(layout.groupArray.ElementAt(i));
                this.stepWizard.addIdControl(layout.groupArray.ElementAt(i).ID);

                this.stepWizard.addComponent(label, this.addValidator(layout.groupArray.ElementAt(i), group));
            }

        }

        /// <summary>
        /// Add a Calendar Component to the current step.
        /// </summary>
        /// <param name="layout"></param>
        private void manageDate(Layout layout)
        {

            for (int i = 0; i < layout.dateArray.Length; i++)
            {
                Calendar cal = (Calendar) createControl.obtainControl(layout.dateArray.ElementAt(i));
                this.stepWizard.addIdControl(layout.dateArray.ElementAt(i).ID);

                this.stepWizard.addComponent(this.addValidator(layout.dateArray.ElementAt(i), cal));
            }

        }

        /// <summary>
        /// Add a Table Component to the current step.
        /// </summary>
        /// <param name="layout"></param>
        private void manageTable(Layout layout)
        {

            for (int i = 0; i < layout.tableArray.Length; i++)
            {
                Table table = (Table) createControl.obtainControl(layout.tableArray.ElementAt(i));
                this.stepWizard.addIdControl(layout.tableArray.ElementAt(i).ID);

                this.stepWizard.addComponent(this.addValidator(layout.tableArray.ElementAt(i), table));
            }

        }

        /// <summary>
        /// Add a Validation component to the WebControl object if it is necessary.
        /// </summary>
        /// <param name="component"></param>
        /// <param name="control"></param>
        /// <returns></returns>
        private WebControl addValidator( WizardComponent component, WebControl control)
        {
            if (component.validate != null)
            {
                return validator.applyValidation(component, control);
            }
            else
            {
                control.ID = component.ID;
            }


            return control;
        }

    }
}
